/**
 * 
 */
package de.htwg.ppt2html5.export;


import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.docx4j.openpackaging.parts.WordprocessingML.ImageJpegPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.htwg.ppt2html5.extraction.Interpreter;


/**
 * @author Roman Allenov
 */
public class Export implements IExport {

   private static Logger log   = LoggerFactory.getLogger(Interpreter.class);

   // -1: initial value; 0: OK; 1: Failure
   private int           error = -1;


   public Export() {
   }


   /**
    * initiates export
    */
   public int export(String doc, String css, File fileName, ImageJpegPart bg)
   {
      return writeFile(doc, css, fileName, bg);
   }


   /**
    * writes the html string in the specified file, as well as the CSS file
    * 
    * @param doc
    *        html file
    * @param css
    *        css file
    * @param file
    *        file to save
    * @param bg
    *        background image
    */
   private int writeFile(String doc, String css, File file, ImageJpegPart bg)
   {
      File htmlFile = new File(file.toPath().toString());
      String fileName = file.getName().substring(0, file.getName().indexOf("."));

      log.info(htmlFile.getPath());
      File cssFile = new File(file.getParent() + "\\css\\default.css");
      log.info(cssFile.getPath());
      File bgFile = new File(file.getParent() + "\\background-" + fileName + ".jpeg");
      log.info(bgFile.getPath());

      BufferedWriter htmlWriter;
      BufferedWriter cssWriter;
      BufferedImage image;

      try {
         if (cssFile.exists()) {
            cssFile.delete();
            cssFile.createNewFile();
         }
         else {
            cssFile.createNewFile();
         }
         if (bgFile.exists()) {
            bgFile.delete();
            bgFile.createNewFile();
         }
         else {
            bgFile.createNewFile();
         }

         htmlWriter = new BufferedWriter(new FileWriter(htmlFile));
         htmlWriter.write(doc);
         htmlWriter.flush();
         htmlWriter.close();
         cssWriter = new BufferedWriter(new FileWriter(cssFile));
         cssWriter.write(css);
         cssWriter.flush();
         cssWriter.close();
         image = ImageIO.read(new ByteArrayInputStream(bg.getBytes()));
         ImageIO.write(image, "jpeg", bgFile);

         setError(0);
      }
      catch (IOException e) {
         setError(1);
         log.warn("Error writing HTML file. {}", e.getMessage());
      }
      return getError();
   }


   /**
    * returns the error code
    */
   public int getError()
   {
      return error;
   }


   public void setError(int error)
   {
      this.error = error;
   }
}
